import {Category, Home} from "../utils/api";//调用api里的路由地图
export default {//创建并导出vuex
    namespaced: true,//可以被访问自己的名字
    state: {//存放保存在仓库中的数据
        list:[],
        isInit:false
    },
    getters:{
        beforePayment(state){//待付款
            if(state.list.length){
                let list = state.list.filter((item)=>{
                    if(!item.status){
                        let product = item.product.split("@");
                        let productList = [];
                        let price = null;
                        product.filter((idx)=>{
                            let i = idx.split(",");
                            if(i.length > 1){
                                price += parseInt(i[2]) * parseInt(i[3]);
                                productList.push(i)
                            }
                        });
                        productList.push(price);
                        item.product = [];
                        item.product.push(productList);
                        return item;
                    }
                });
                return list;
            }else{
                return [];
            }
        },
        afterPayment(state){//已付款待收货
            if(state.list.length){
                let list = state.list.filter((item)=>{
                    if(item.status){
                        let product = item.product.split("@");
                        let productList = [];
                        let price = null;
                        product.filter((idx)=>{
                            let i = idx.split(",");
                            if(i.length > 1){
                                price += parseInt(i[2]) * parseInt(i[3]);
                                productList.push(i)
                            }
                        });
                        productList.push(price);
                        item.product = [];
                        item.product.push(productList);
                        return item;
                    }
                });
                return list
            }else{
                return [];
            }
        }
    },
    mutations: {//更改state中的数据，只有mutations有资格
        _init(state,payload){
            state.list = payload.data;
            state.isInit = true;
        }
    },
    actions: {//执行一些异步耗时操作，比如请求数据
        init({state, commit, rootState: {$http}},payload){//rootState总仓库的$http解构
            $http.post("/profile",{user:payload})
                .then(result => commit("_init", result));
        },
    },
};